package com.phuc.game.andPuzzle;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.phuc.game.andPuzzle.common.Utils;

public class MainActivity extends Activity implements OnItemClickListener{
	ArrayList<LevelObject> list;
	ListView lv;
	TextView tv_title;
	BaseAdapter adapter;
	private AdView adView;
	
	public static int [] points;
	public static boolean hasChanged;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		hasChanged = false;
		Typeface tf = Typeface.createFromAsset(getAssets(),
	            "fonts/victoria.ttf");
		setContentView(R.layout.activity_main);
		
		tv_title = (TextView) findViewById(R.id.textView1);
		tv_title.setTypeface(tf);
		list = new ArrayList<LevelObject>();
		addData();
		lv = (ListView)findViewById(R.id.listView1);
		adapter = new LevelAdapter(this, list);
		lv.setAdapter(adapter);
		
		lv.setOnItemClickListener(this);
		
		// Get stage
		String stageString = Utils.getPreferenceString("stagePoint", this);
		if (Utils.isNullOrEmpty(stageString)) {
			points = new int[0];
		} else {
			String [] strPoints = stageString.split(";");
			points = new int[strPoints.length];
			for (int i = 0; i < points.length; i++) {
				points[i] = Integer.parseInt(strPoints[i]);
			}
		}
		// Load Ads
	    adView = (AdView) findViewById(R.id.adView);
	    new Thread() {
        	public void run() {
        		adView.loadAd(new AdRequest());
        	}
        }.start();
        adView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				adView.setVisibility(View.INVISIBLE);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		if (position < points.length + 1) {
			Intent i = new Intent(MainActivity.this,ImageActivity.class);
			i.putExtra("position", Integer.toString(position));
			startActivityForResult(i, 1);
		} else {
			Toast.makeText(this, "Màn này chưa chơi được", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void addData(){
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
		list.add(new LevelObject("1", "1", "1"));
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (hasChanged) {
			String saveString = "";
			for (int i = 0; i < points.length - 1; i++) {
				saveString += points[i] + ";";
			}
			saveString += points[points.length - 1];
			hasChanged = false;
			adapter.notifyDataSetChanged();
			Utils.savePreferenceString("stagePoint", saveString, this);
		}
		if (requestCode == 2000) {
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, MoreGameActivity.class);
		startActivityForResult(intent, 2000);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (adView != null) {
			adView.destroy();
		}
		Utils.unbindDrawable(findViewById(R.id.root));

	}
}
